package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysRes;
import cn.wgx.modules.sys.services.SysResService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Api(tags = "9. 查看上传资源")
@RestController
@RequestMapping(path = "api/sysRes")
public class SysResController extends BaseController{

    @Resource
    SysResService service;

    @ApiOperation(value = "列表", notes = "" )
    @ApiPermission
    @RequestMapping(path = "list", method = RequestMethod.GET)
    public Map<String, Object> getList(
            @ModelAttribute SysRes record, 
			@RequestParam(value = "page", required = false, defaultValue = "1")Integer page,
			@RequestParam(value = "pageSize", required = false, defaultValue = "20")Integer pageSize,
            @ApiParam(value = "是否倒序,默认是")@RequestParam(defaultValue = "true", required = false)Boolean desc
        ) {
		return returnData(PageHelper.startPage(page,  Math.min(Math.abs(pageSize), 200)), service.getList(record, desc));
    }

    @ApiOperation(value = "验证资源是否可使用", notes = "" )
    @RequestMapping(path = "auth", method = RequestMethod.GET)
    public Map<String, Object> auth(HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        if(subject != null){
            if(subject.isRemembered() || subject.isAuthenticated()){
                return returnData(1);
            }
        }
        response.setStatus(401);
        return returnData(1);
    }

	
    @ApiOperation(value = "获取单个", notes = "" )
    @ApiPermission
	@RequestMapping(path = "get", method = RequestMethod.GET)
    public Map<String, Object> getOne(Integer id) {
		return returnData(service.getOne(id));
    }



    @ApiOperation(value = "删除", notes = "" )
    @ApiPermission
    @RequestMapping(path = "del/{id}", method = RequestMethod.DELETE)
    public Map<String, Object> deleteOne(@PathVariable("id") Integer id) {
        return returnData(service.deleteOne(id));
    }

	

}